import { getTokenByCode} from '/api/user'
import Event from '/utils/Event';
const util = require('/utils/util.js')

App({
    event: new Event(),
    onLaunch: function (options) {
        var _this = this;
        _this.screenSize(); //获取可视窗口的尺寸
        wx.getSystemInfo({
            success: function (res) {
                _this.globalData.rpxR = 750 / res.windowWidth;
                _this.globalData.windowWidth = res.windowWidth * _this.globalData.rpxR;
                _this.globalData.windowHeight = res.windowHeight * _this.globalData.rpxR;
                _this.globalData.statusBarHeight = res.statusBarHeight * _this.globalData.rpxR;
            }
        })
        _this.globalData.uuid = util.getUUID()
       let token =  wx.getStorageSync('token')
        if(token == "" || token == null){
            wx.login({
                success: res => {
                    _this.globalData.openId = res.code
                    getTokenByCode({
                        auth_code: res.code,
                        mchId: _this.globalData.mchId,
                        uuid: _this.globalData.uuid,
                        pluginId: 'weixinMiniAuthPlugin'
                    }).then( res => {
                            if (res.type == 'success') {
                                const {
                                    userInfo,
                                    userId,
                                    token
                                } = res.data
                                wx.setStorageSync('token', token);
                                _this.globalData.token = wx.getStorageSync('token')
                                _this.globalData.uuid = userId
                                _this.globalData.openId = userId
                                _this.globalData.userInfo = userInfo
                            }
                            if (_this.userInfoReadyCallback) {
                                _this.globalData.loaded = true;
                                _this.userInfoReadyCallback(res)
                            }
                        }
                    )
                },
                fail: err => {
                    if (_this.APP_TYPE > 0) {
                        if (_this.userInfoReadyCallback) {
                            _this.globalData.loaded = true;
                            _this.userInfoReadyCallback(err)
                        }
                    } else {
                        if (_this.userInfoReadyCallback) {
                            _this.globalData.loaded = true;
                            _this.userInfoReadyCallback(err)
                        }
                    }
                }
            })
        }else{
             this.globalData.token = wx.getStorageSync('token')
        }

    },
    APP_BASE_API:'https://oil.rzico.com:8443',
    // APP_BASE_API: 'https://api.rzico.com:8443',
    // APP_BASE_API: 'https://boot.rzico.com:9090',
    // APP_BASE_API: 'https://guo.rzico.com:8443',

    ISV_ID: '10200',
    SHARE_TYPE:0, //0:公共流量   1：私域流量
    FUNS_TYPE:"0",
    globalData: {
        loaded:false,
        mchName:'反诈骗',  
        mchId: '10200',  //反诈骗 wx940012f98256d746
        openId:null,
        uuid:null,
    },
    //获取可视窗口宽度
    screenSize: function () {
        var that = this;
        wx.getSystemInfo({
            success: function (res) {
                //可视窗口宽度
                var ww = res.windowWidth;
                //可视窗口高度
                var hh = res.windowHeight;
                that.globalData.ww = ww;
                that.globalData.hh = hh;
            }
        })
    },
    watch: function (dataName, callback, that) {
        // 拿到全局变量、当然你也可以监听任何其他的对象
        var obj = this.globalData;
        Object.defineProperty(obj, dataName, {
            // 可配置
            configurable: true,
            // 可枚举
            enumerable: true,
            // 当监听的函数被设置新值的时候执行
            set: function (value) {
                // 当我们监听的值被修改的时候，触发 set 方法
                // 把新值存入一个临时的属性里，为什么存到临时的？看后边。
                this['temp' + dataName] = value;
                // 调用回调函数，传入新值
                callback.call(that, value);
            },
            // 当监听的函数被get的时候调用
            get: function () {
                // 当我们主动去 get 监听的这个值的时候触发当前函数，这里你可以进行一下其他操作。
                return this['temp' + dataName];
            }
        })
    }

})
